// 名字列表页
var api = require("../../utils/api.js")
Page({
  data: {
    curView: 'solo',
    cue: 0,
    user: {},
    info: {},
    id: '',
    name: '单人赛',
    loading: true
  },
  // 切换tab
  changeView: function(event) {
    let self = this;
    let name = '单人赛';
    let target = event.target;
    let dataset = event.target.dataset;
    switch (dataset.view) {
      case 'solo':
        self.data.curView = 'solo';
        name = '单人赛';
        break;
      case 'duo':
        self.data.curView = 'duo';
        name = '双人赛';
        break;
      case 'squad':
        self.data.curView = 'squad';
        name = '团队赛';
        break;
    }

    this.setData({
      curView: this.data.curView,
      name: name
    })
  },
  back() {
    wx.redirectTo({
      url: '../index/index'
    })
  },
  update() {
    let self = this;
    let id = this.data.id
    this.setData({
      loading: false,
    })
    api.http("pubg/updateGameUserInfo", "POST", {
      encryptedData: wx.getStorageSync('encryptedData'),
      iv: wx.getStorageSync('iv'),
      userId: id
    }).then((res) => {
      if (res.data.code == 1) {
        self.getInfo(id);
        wx.showToast({
          title: '更新成功',
          icon: 'success',
          duration: 1000
        });
      }else{
        self.setData({
          loading:true
        })
      }
    })
  },
  // 相关信息
  getInfo(id) {
    let self = this;
    let bindStatus = false;
    let scene = wx.getStorageSync('scene');

    let encryptedData = wx.getStorageSync('encryptedData')
    let iv = wx.getStorageSync('iv')
    this.setData({
      loading: false,
    })
    api.http("pubg/getUserInfo", "POST", {
      encryptedData: encryptedData,
      iv: iv,
      userId: id
    }).then((res) => {
      if (res.data.code == 1) {
        let data = res.data.datas;
        if (data.pubg) {
          let info = data.pubg;
          self.setData({
            user: data.wechat,
            info: info,
            loading: true
          })
        } else {
          self.setData({
            loading: true
          })
        }
      } else {
        self.setData({
          loading: true
        })
      }
    })
  },
  getFriendInfo(stid) {
    let self = this;
    let bindStatus = false;
    let scene = wx.getStorageSync('scene');
    this.setData({
      loading: false,
    })
    api.http("pubg/getFriendInfo", "POST", {
      steamId: stid
    }).then((res) => {
      if (res.data.code == 1) {
        let data = res.data.datas;
        if (data.pubg) {
          let info = data.pubg;
          self.setData({
            user: data.wechat,
            info: info,
            loading: true
          })
        } else {
          self.setData({
            loading: true
          })
        }
      } else {
        self.setData({
          loading: true
        })
      }
    })
  },
  // 分享
  onShareAppMessage: function() {
    return {
      title: '绝地求生群排行',
      path: 'pages/index/index'
    }
  },
  onLoad: function(options) {
    let id = options.id
    let stid = options.stid
    let name = options.name
    if (name == 'friend') { name = 'solo' }
    this.data.id = id
    if (id) {
      this.getInfo(id)
    } else if (stid) {
      this.getFriendInfo(stid)
    }
    this.setData({
      curView: name
    })
    wx.setNavigationBarTitle({
      title: '玩家详情'
    })
  }
})